#实现异或门
import numpy as np

def AND(x1, x2):  #定义与门
    x = np.array([x1, x2])  #输入
    w = np.array([0.5, 0.5])  #权重
    b = -0.7  #偏置
    temp = np.sum(w*x) + b
    if temp <=0:
        return 0
    else:
        return 1

def NAND(x1, x2):  #定义与非门
    x = np.array([x1, x2])  #输入
    w = np.array([-0.5, -0.5])  #权重
    b = 0.7  #偏置
    temp = np.sum(w*x) + b
    if temp <=0:
        return 0
    else:
        return 1

def OR(x1, x2):  #定义或门
    x = np.array([x1, x2])  #输入
    w = np.array([0.5, 0.5])  #权重
    b = -0.2  #偏置
    temp = np.sum(w*x) + b
    if temp <=0:
        return 0
    else:
        return 1

def XOR(x1, x2):  #定义异或门
    s1 = NAND(x1, x2)  #调用与非门
    s2 = OR(x1,x2)  #调用或门
    y = AND(s1, s2) #调用与门
    return y

data = np.array([[0, 0], [1, 0], [0, 1], [1, 1]])
#实现与门
print('*' * 10)
for element in data:
    result = AND(element[0], element[1])
    print('实现与门: ' + str(element) + ' -> ' + str(result))

#实现与非门
print('*' * 10)
for element in data:
    result = NAND(element[0], element[1])
    print('实现与非门: ' + str(element) + ' -> ' + str(result))

#实现或门
print('*' * 10)
for element in data:
    result = OR(element[0], element[1])
    print('实现或门: ' + str(element) + ' -> ' + str(result))

#实现异或门
print('*' * 10)
for element in data:
    result = XOR(element[0], element[1])
    print('实现异或门: ' + str(element) + ' -> ' + str(result))
